package ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.presentation.controller.user;

import javax.faces.application.NavigationHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;

import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.service.SystemService;
import ar.edu.utn.frsf.proyectofinal.sistemadecontrolmodbus.service.UserDataService;


@ManagedBean(name="usuarioBean")
@SessionScoped
public class UsuarioBean {
	
	@ManagedProperty(value="#{systemService}")
	private SystemService logInService;
	
	
	private UserDataService userDataService;
	
	public void logIn(String username, String password) {
		userDataService = logInService.logIn(username, password);
	}
	
	public void logout(){
		FacesContext.getCurrentInstance().getExternalContext().getSessionMap().clear();
	}
	
	public void authoritation(ComponentSystemEvent cse) {

		FacesContext faceContext = FacesContext.getCurrentInstance();
		String viewId = faceContext.getViewRoot().getViewId();
		NavigationHandler navigHand =  faceContext.getApplication().getNavigationHandler();
		if (!isLogged() ) {
			
			if(!viewId.startsWith("/login") && !viewId.startsWith("/systemdown")) {
				
				navigHand.handleNavigation(faceContext, null,	"/login?faces-redirect=true");
			}
			
		}
		else{
			
			if(viewId.startsWith("/login")){
				
				String direct ="/";
				if(userDataService.isAdmin())
					direct = "/adminuser/main";
				if(userDataService.isSimpleUser())
					direct = "/simpleuser/main";
				navigHand.handleNavigation(faceContext, null,	direct + "?faces-redirect=true");
			
			}
			else if(!viewId.startsWith("/adminuser") &&  userDataService.isAdmin()){
				
				navigHand.handleNavigation(faceContext, null,	"/adminuser/main?faces-redirect=true");
			
			}
			else if( userDataService.isSimpleUser()){
				 
				 if(!logInService.isActive()){
					 
					FacesContext.getCurrentInstance().getExternalContext().getSessionMap().clear();
					navigHand.handleNavigation(faceContext, null,"/systemdown?faces-redirect=true");
				
				 }
				 else if(!viewId.startsWith("/simpleuser")){
					 
					 navigHand.handleNavigation(faceContext, null,	"/simpleuser/main?faces-redirect=true");
				 
				 }
		    }
		
		}
	}
	
	public boolean isLogged(){
		return this.userDataService != null;
	}
	
	public boolean isAdmin(){
		if(this.userDataService != null){
			return userDataService.isAdmin();
		}
		return false;
	}

	public boolean isSimpleUser(){
		if(this.userDataService != null){
			return userDataService.isSimpleUser();
		}
		return false;
	}

	
	/**
	 * @return the logInService
	 */
	public SystemService getLogInService() {
		return logInService;
	}

	/**
	 * @param logInService the logInService to set
	 */
	public void setLogInService(SystemService logInService) {
		this.logInService = logInService;
	}


	/**
	 * @return the userDataService
	 */
	public UserDataService getUserDataService() {
		return userDataService;
	}

	/**
	 * @param userDataService the userDataService to set
	 */
	public void setUserDataService(UserDataService userDataService) {
		this.userDataService = userDataService;
	}

}
